home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / modelgraphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-13  |  3.2 KB  |  135 lines  |  [TEXT/CWIE]

  1. #include "model.h"
  2.  
  3. #include "GL/gl.h"
  4. #include <stdio.h>
  5.  
  6. #ifdef POLY_COUNT
  7. extern int polycount;
  8. #endif
  9.  
  10. void drawMeshPart(MeshPart* meshpart, int flag) {
  11.   int i, j;
  12.   int type, c;
  13.   float *normal;
  14.   float *vertex;
  15.  
  16.   for(i = 0; i < meshpart->nFaces; i++) {
  17.     c = *(meshpart->facesizes + i);
  18.     type = 0;
  19.     if(c > 4) type = GL_POLYGON;
  20.     if(c == 4) type = GL_QUADS; 
  21.     if(c == 3) type = GL_TRIANGLES;
  22.     if(flag & 1) type = GL_LINE_LOOP;
  23.     if(type != 0) {
  24.       glBegin(type);
  25.       for(j = 0; j < c; j++) {
  26.     normal = meshpart->normals + 3 * (i * MODEL_FACESIZE + j);
  27.     glNormal3fv(normal);
  28.     vertex = meshpart->vertices + 3 * (i * MODEL_FACESIZE + j);
  29.     glVertex3fv(vertex);
  30.       }
  31.       glEnd();
  32. #ifdef POLY_COUNT
  33.       polycount += c - 2
  34. #endif
  35. ;
  36.     } 
  37.   }
  38. }
  39.  
  40. void drawExplosionPart(MeshPart* meshpart, float radius, int flag) {
  41.   int i, j;
  42.   int type, c;
  43.   float *normal;
  44.   float *vertex;
  45.  
  46. #define EXP_VECTORS 10
  47.   float vectors[][3] = {
  48.     { 0.03, -0.06, -0.07 }, 
  49.     { 0.04, 0.08, -0.03 }, 
  50.     { 0.10, -0.04, -0.07 }, 
  51.     { 0.06, -0.09, -0.10 }, 
  52.     { -0.03, -0.05, 0.02 }, 
  53.     { 0.07, 0.08, -0.00 }, 
  54.     { 0.01, -0.04, 0.10 }, 
  55.     { -0.01, -0.07, 0.09 }, 
  56.     { 0.01, -0.01, -0.09 }, 
  57.     { -0.04, 0.04, 0.02 }
  58.   };
  59.  
  60.  
  61.   for(i = 0; i < meshpart->nFaces; i++) {
  62.     c = *(meshpart->facesizes + i);
  63.     type = 0;
  64.     if(c > 4) type = GL_POLYGON;
  65.     if(c == 4) type = GL_QUADS; 
  66.     if(c == 3) type = GL_TRIANGLES;
  67.     if(flag & 1) type = GL_LINE_LOOP;
  68.     if(type != 0) {
  69.       glPushMatrix();
  70.       normal = meshpart->normals + 3 * (i * MODEL_FACESIZE);
  71.       glTranslatef(radius * (*(normal + 0) + vectors[i % EXP_VECTORS][0]),
  72.            radius * (*(normal + 1) + vectors[i % EXP_VECTORS][1]),
  73.            radius * (*(normal + 2) + vectors[i % EXP_VECTORS][2]) );
  74.  
  75.       glBegin(type);
  76.       for(j = 0; j < c; j++) {
  77.     normal = meshpart->normals + 3 * (i * MODEL_FACESIZE + j);
  78.     glNormal3fv(normal);
  79.     vertex = meshpart->vertices + 3 * (i * MODEL_FACESIZE + j);
  80.     glVertex3fv(vertex);
  81.       }
  82.       glEnd();
  83. #ifdef POLY_COUNT
  84.       polycount += c - 2;
  85. #endif
  86.       glPopMatrix();
  87.     } 
  88.   }
  89. }
  90.  
  91. void printColor(float *values, int count) {
  92.   int i;
  93.   printf("color: ");
  94.   for(i = 0; i < count; i++)
  95.     printf("%.2f", *(values + i));
  96.   printf("\n");
  97. }
  98.  
  99.  
  100. void drawModel(Mesh *mesh, int mode, int flag) {
  101.   int i;
  102.  
  103.   for(i = 0; i < mesh->nMaterials; i++) {
  104.     /* set materials */
  105.     if(mode & MODEL_USE_MATERIAL) {
  106.       glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 50);
  107.       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,
  108.            (mesh->materials + i)->ambient);
  109.  
  110.       glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,
  111.            (mesh->materials + i)->diffuse);
  112.  
  113.       glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
  114.            (mesh->materials + i)->specular);
  115.     }
  116.     drawMeshPart(mesh->meshparts + i, flag);
  117.   }
  118. }
  119.  
  120. void drawExplosion(Mesh *mesh, float radius, int mode, int flag) {
  121.   int i;
  122.   /* printf("radius: %.2f\n", radius); */
  123.   for(i = 0; i < mesh->nMaterials; i++) {
  124.       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,
  125.            (mesh->materials + i)->ambient);
  126.  
  127.       glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,
  128.            (mesh->materials + i)->diffuse);
  129.  
  130.       glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
  131.            (mesh->materials + i)->specular);
  132.       drawExplosionPart(mesh->meshparts + i, radius, flag);
  133.   }
  134. }
  135.